'use strict';

import React, { Component } from 'react';
import { DatePicker, Button } from 'antd';
import moment from 'moment';
let { RangePicker } = DatePicker;
import { Relax } from 'iflux2';
const dateFormat = 'YYYY-MM-DD';

@Relax
export default class RangeDatePicker extends Component {
  static defaultProps = {
    addCondition: () => {}
  };

  constructor(props) {
    super(props);
    this.state = {
      from: '',
      to: ''
    };
  }

  render() {
    let { from, to } = this.state;
    let value = from && to ? [moment(from, dateFormat), moment(to, dateFormat)] : [];
    return (
      <span className="extend-func">
        <RangePicker onChange={this._changeRangeDate} value={value} disabledDate={this._disabledDate} />
        <Button type="primary" onClick={this._returnResult}>确定</Button>
      </span>
    );
  }

  _disabledDate(current) {
    return current && current.valueOf() > Date.now();
  }

  _changeRangeDate = (date, dateString) => {
    let [from, to] = dateString;
    this.setState({ from, to });
  };

  _returnResult = () => {
    let { from, to } = this.state;
    if (!from || !to) {
      return;
    }
    let { demo, path } = this.props;
    Object.assign(demo, { value: { from, to }, label: `${from} - ${to}` });
    this.props.addCondition({ value: demo, path });
    this.setState({ from: '', to: '' });
  };
}
